@using Microsoft.AspNetCore.SignalR.Client
@inject IJSRuntime JS
@inject NavigationManager Navigation
@implements IAsyncDisposable

<div class="admin-notifications" style="position: fixed; top: 20px; right: 20px; z-index: 2000;">
    @foreach (var notification in Notifications)
    {
        <div class="notification-card @(notification.Type)">
            <div class="notification-header">
                <h4>@notification.Title</h4>
                <button class="close-btn" @onclick="() => RemoveNotification(notification.Id)">×</button>
            </div>
            <div class="notification-content">
                @notification.Content
            </div>
            <div class="notification-time">
                @notification.Timestamp.ToString("HH:mm:ss")
            </div>
        </div>
    }
</div>

<style>
    .admin-notifications {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    .notification-card {
        background-color: rgba(20, 20, 40, 0.95);
        border: 2px solid #4a2c8d;
        border-radius: 10px;
        padding: 15px;
        width: 300px;
        box-shadow: 0 0 20px rgba(106, 13, 173, 0.7);
        animation: slideIn 0.3s ease-out;
    }

    .notification-card.副本创建 {
        border-color: #0abda7;
    }

    .notification-card.通知 {
        border-color: #6a0dad;
    }

    .notification-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 10px;
    }

    .notification-header h4 {
        color: #b19cd9;
        margin: 0;
        font-size: 1.1rem;
    }

    .close-btn {
        background: none;
        border: none;
        color: #a0a0e0;
        font-size: 1.2rem;
        cursor: pointer;
        padding: 0;
        width: 24px;
        height: 24px;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .close-btn:hover {
        color: #ff5555;
    }

    .notification-content {
        color: #e0e0ff;
        margin-bottom: 10px;
        font-size: 0.9rem;
    }

    .notification-time {
        color: #a0a0e0;
        font-size: 0.8rem;
        text-align: right;
    }

  
</style>

@code {
    public class Notification
    {
        public string Id { get; set; } = Guid.NewGuid().ToString();
        public string Title { get; set; } = "";
        public string Content { get; set; } = "";
        public DateTime Timestamp { get; set; }
        public string Type { get; set; } = "通知";
    }

    private List<Notification> Notifications { get; set; } = new List<Notification>();
    
    // SignalR连接
    private HubConnection? connection;

    protected override async Task OnInitializedAsync()
    {
        // 初始化SignalR连接
        await InitializeSignalR();
    }

    // 初始化SignalR连接
    private async Task InitializeSignalR()
    {
        connection = new HubConnectionBuilder()
            .WithUrl(Navigation.ToAbsoluteUri("/gamehub"))
            .Build();

        // 注册接收管理员通知的方法
        connection.On<dynamic>("接收管理员通知", (notification) =>
        {
            var newNotification = new Notification
            {
                Title = notification.标题,
                Content = notification.内容,
                Timestamp = notification.时间戳,
                Type = notification.类型
            };

            Notifications.Add(newNotification);
            InvokeAsync(StateHasChanged);

            // 5秒后自动移除通知
            Task.Delay(5000).ContinueWith(_ =>
            {
                RemoveNotification(newNotification.Id);
                InvokeAsync(StateHasChanged);
            });
        });

        try
        {
            await connection.StartAsync();
            Console.WriteLine("管理员通知连接已建立");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"管理员通知连接失败: {ex.Message}");
        }
    }

    private void RemoveNotification(string id)
    {
        var notification = Notifications.FirstOrDefault(n => n.Id == id);
        if (notification != null)
        {
            Notifications.Remove(notification);
            StateHasChanged();
        }
    }

    // 实现IAsyncDisposable接口，确保正确释放资源
    public async ValueTask DisposeAsync()
    {
        if (connection != null)
        {
            await connection.StopAsync();
            await connection.DisposeAsync();
        }
    }
}